package day52.pet.service.impl;

import day52.pet.domain.PetType;
import day52.pet.mapper.PetMapper;
import day52.pet.mapper.PetTypeMapper;
import day52.pet.service.IPetTypeService;
import day52.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yang
 * @since 2022-12-27
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> queryTree() {
        //查询出所有的petType
        List<PetType> allPetTypes = petTypeMapper.findAll();
        HashMap<Long, PetType> hm = new HashMap<>();
        //结果集
        List<PetType> resultPetType = new ArrayList<>();;
        for (PetType allPetType : allPetTypes) {
            hm.put(allPetType.getId(),allPetType);
        }
        for (PetType petType : allPetTypes) {
            //找出所有的爹
            if(petType.getPid() == null){
                resultPetType.add(petType);
            }else{
                //找出所有的儿子塞到爹的children里 petType在这里就是子部门了
                PetType parent = hm.get(petType.getPid());
                //把儿子塞到爹的children里去
                parent.getChildren().add(petType);
            }
        }
        return resultPetType;
    }
}
